<!DOCTYPE html>
<html>
<head>
<title>RC Circuit and Capacitor Diagram</title>
<style>
  body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #f0f0f0;
  }
  canvas {
    border: 1px solid #ccc;
    background-color: #fff;
  }
</style>
</head>
<body>
<canvas id="canvas" width="450" height="420"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');

// Style settings
ctx.strokeStyle = 'black';
ctx.fillStyle = 'black';
ctx.lineWidth = 2.5;
ctx.lineCap = 'butt'; 
ctx.lineJoin = 'miter'; 


// --- Draw Circuit Diagram ---

const cx = 225; // center x
const cy = 130; // center y
const w = 250;  // width of the circuit loop
const h = 200;  // height of the circuit loop
const leftX = cx - w / 2;
const rightX = cx + w / 2;
const topY = cy - h / 2;
const bottomY = cy + h / 2;

// AC Source
ctx.beginPath();
const sourceRadius = 22;
const sourceCenterY = topY + h / 2;
ctx.arc(leftX, sourceCenterY, sourceRadius, 0, 2 * Math.PI);
ctx.stroke();

// Sine wave inside the source
ctx.beginPath();
ctx.moveTo(leftX - sourceRadius * 0.7, sourceCenterY);
ctx.quadraticCurveTo(leftX - sourceRadius * 0.35, sourceCenterY - sourceRadius * 0.8, leftX, sourceCenterY);
ctx.quadraticCurveTo(leftX + sourceRadius * 0.35, sourceCenterY + sourceRadius * 0.8, leftX + sourceRadius * 0.7, sourceCenterY);
ctx.stroke();

// Wires
ctx.beginPath();
// Top wire
ctx.moveTo(leftX, topY);
ctx.lineTo(rightX, topY);
// Bottom wire
ctx.moveTo(leftX, bottomY);
ctx.lineTo(rightX, bottomY);
// Left wire segments
ctx.moveTo(leftX, topY);
ctx.lineTo(leftX, sourceCenterY - sourceRadius);
ctx.moveTo(leftX, sourceCenterY + sourceRadius);
ctx.lineTo(leftX, bottomY);
ctx.stroke();


// Right branch components
// Capacitor 'C'
const capY1 = topY + 40;
const capY2 = topY + 65;
const capPlateWidth = 35;
// Wire to capacitor
ctx.beginPath();
ctx.moveTo(rightX, topY);
ctx.lineTo(rightX, capY1);
ctx.stroke();
// Capacitor plates
ctx.beginPath();
ctx.moveTo(rightX - capPlateWidth / 2, capY1);
ctx.lineTo(rightX + capPlateWidth / 2, capY1);
ctx.moveTo(rightX - capPlateWidth / 2, capY2);
ctx.lineTo(rightX + capPlateWidth / 2, capY2);
ctx.stroke();
// Label C
ctx.font = '24px serif';
ctx.fillText('C', rightX + capPlateWidth / 2 + 10, (capY1 + capY2) / 2 + 8);


// Resistor 'R'
const resTopY = capY2 + 45;
const resHeight = 50;
const resBottomY = resTopY + resHeight;
const resWidth = 20;
// Wire from capacitor to resistor
ctx.beginPath();
ctx.moveTo(rightX, capY2);
ctx.lineTo(rightX, resTopY);
ctx.stroke();
// Resistor zigzag
ctx.beginPath();
ctx.moveTo(rightX, resTopY);
ctx.lineTo(rightX - resWidth / 2, resTopY + resHeight * 1 / 6);
ctx.lineTo(rightX + resWidth / 2, resTopY + resHeight * 2 / 6);
ctx.lineTo(rightX - resWidth / 2, resTopY + resHeight * 3 / 6);
ctx.lineTo(rightX + resWidth / 2, resTopY + resHeight * 4 / 6);
ctx.lineTo(rightX - resWidth / 2, resTopY + resHeight * 5 / 6);
ctx.lineTo(rightX, resBottomY);
ctx.stroke();
// Wire from resistor to bottom
ctx.beginPath();
ctx.moveTo(rightX, resBottomY);
ctx.lineTo(rightX, bottomY);
ctx.stroke();
// Label R
ctx.fillText('R', rightX + resWidth / 2 + 10, resTopY + resHeight / 2 + 8);


// --- Draw Capacitor Schematic ---

const barY = 320;
const barHeight = 40;
const barStartX = 80;
const barEndX = 370;
const dividerX = 210;

// Draw the outline
ctx.beginPath();
ctx.moveTo(barStartX, barY);
// Left part (open end)
ctx.lineTo(dividerX, barY);
ctx.moveTo(barStartX, barY + barHeight);
ctx.lineTo(dividerX, barY + barHeight);
// Divider
ctx.moveTo(dividerX, barY);
ctx.lineTo(dividerX, barY + barHeight);
// Right part
ctx.rect(dividerX, barY, barEndX - dividerX, barHeight);
ctx.stroke();

// Draw the hatching in the right part
ctx.save();
ctx.beginPath();
ctx.rect(dividerX, barY, barEndX - dividerX, barHeight);
ctx.clip();

ctx.beginPath();
const spacing = 18;
const hatchWidth = barEndX - dividerX;
const totalWidth = hatchWidth + barHeight;
for (let i = 0; i < totalWidth / spacing; i++) {
    const startX = dividerX + i * spacing;
    ctx.moveTo(startX, barY + barHeight);
    ctx.lineTo(startX - barHeight, barY);
}
ctx.lineWidth = 2;
ctx.stroke();
ctx.restore();


// --- Draw Caption ---
ctx.font = 'bold 20px serif';
ctx.textAlign = 'center';
ctx.fillText("Fig. 1.22", cx, 390);

</script>
</body>
</html>